package com.yogee.topseller.adapter;

import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.BeginQueryActivity;
import com.yogee.topseller.activity.FlashDetailsActivity;
import com.yogee.topseller.bean.FastQueryBean;
import com.yogee.topseller.view.FlowLayout;
import com.yogee.topseller.view.FlowLayoutSearch;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/7/14.
 */

public class FlashRvAdapter extends RecyclerView.Adapter {

    private Context context;
    private int TYPE_ITEM = 0, TYPE_BOTTOM = 1;
    private List<FastQueryBean.QueryListBean> data;
    private String tel;

    public FlashRvAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == data.size()) {
            return TYPE_BOTTOM;
        } else {
            return TYPE_ITEM;
        }
    }

    public void addData(List<FastQueryBean.QueryListBean> data,String tel) {
        this.data = data;
        this.tel = tel;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_flash, parent, false);
            return new ItemHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.bottom_flash, parent, false);
            return new BottomHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ItemHolder) {
            ((ItemHolder) holder).flashLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(new Intent(context, FlashDetailsActivity.class).putExtra("flashId", data.get(position).getId()));
//                    context.startActivity(new Intent(context, Demo.class).putExtra("flashId", data.get(position).getId()));
                }
            });

            // 编辑
            ((ItemHolder) holder).tv_eidt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(new Intent(context, FlashDetailsActivity.class).putExtra("flashId", data.get(position).getId()));
                }
            });

//            ((ItemHolder) holder).flashRl.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    context.startActivity(new Intent(context, BeginQueryActivity.class).putExtra("id", data.get(position).getId()));
//                }
//            });

            ((ItemHolder) holder).item_flash_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(new Intent(context, BeginQueryActivity.class).putExtra("id", data.get(position).getId()));
                }
            });

            ((ItemHolder) holder).nameTv.setText(data.get(position).getName());
            List<String> s = new ArrayList<>();
            for (int i = 0; i < data.get(position).getLabelList().size(); i++) {
                s.add(data.get(position).getLabelList().get(i).getLabelName());
            }

            ((ItemHolder)holder).fl_flash_data.setDefaultData1(s);
//            addLable(((ItemHolder) holder).labelsLl, s);

        }
        if (holder instanceof BottomHolder) {
            ((BottomHolder) holder).addTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(new Intent(context, FlashDetailsActivity.class).putExtra("flashId", ""));
                }
            });
            // 人工电话
            ((BottomHolder) holder).flash_pipei_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                if(!tel.equals("")){
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + tel);
                    intent.setData(data);
                    context.startActivity(intent);
                }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size() + 1;
    }

    private void addLable(LinearLayout layout, List<String> s) {
        layout.removeAllViews();
        int size = 0;
        String text = "";
        for (int i = 0; i < s.size(); i++) {
            text += s.get(i);
            if (text.length() <= 9) {
                size = i + 1;
            }
        }
        for (int i = 0; i < size; i++) {
            View view = LayoutInflater.from(context).inflate(R.layout.flash_lable_tv, null);
            TextView tv = (TextView) view.findViewById(R.id.flash_lable_tv);
            tv.setText(s.get(i));
            layout.addView(view);
        }
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_flash_name_tv)
        TextView nameTv;

//        @BindView(R.id.item_flash_rl)
//        RelativeLayout flashRl;
        @BindView(R.id.item_flash_ll)
        LinearLayout flashLl;
        @BindView(R.id.tv_eidt)
        TextView tv_eidt;
        @BindView(R.id.item_flash_img)
        ImageView item_flash_img;
        @BindView(R.id.fl_flash_data)
        FlowLayoutSearch fl_flash_data;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.bottom_flash_add_tv)
        TextView addTv;
        @BindView(R.id.flash_pipei_tv)
        TextView flash_pipei_tv;

        public BottomHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
